
import { createStore } from 'vuex'

const moduleA = {

  state: () => {
    return {
      username: '模块A'
    }
  },
  getters: {
    changeName (state) {
      return state.username + '!!!!!AAA'
    }
  }
}

const moduleB = {
  // 带命名空间的模块
  namespaced: true,
  state: () => {
    return {
      username: '模块B'
    }
  },
  getters: {
    changeName (state) {
      return state.username + '!!!!BBB'
    }
  },
  mutations: {
    update (state) {
      state.username = 'BBBB' + state.username
    }
  },
  actions: {
    asyncUpdate (ctx) {
      setTimeout(() => {
        ctx.commit('update')
      }, 2000)
    }
  }
}

export default createStore({
  state: {
    person: [
      { id: 1, name: 'tom', gender: '男' },
      { id: 2, name: 'lucy', gender: '女' },
      { id: 3, name: 'jack', gender: '男' }
    ]

  },
  getters: {
    boys (state) {
      return state.person.filter(p => p.gender === '男')
    }

  },
  mutations: {},
  actions: {},
  modules: {
    moduleA,
    moduleB
  }

})
